//
//  SuperManagerVC.m
//  优学荟管理系统
//
//  Created by 星道 on 17/9/16.
//  Copyright © 2017年 星道三好. All rights reserved.
//

#import "SuperManagerVC.h"
#import "HNSNHomePageCell.h"
#import "HNSNHeaderView.h"
#import "SuperManagerModel.h"
#import "SuperManagerSubCell.h"
#import "ZYSuperManagementSystemController.h"
#import "ZYAccountManagementController.h"
#import "ZYFundManagenmemtController.h"
#import "OrdinaryHomeVC.h"
#import "ModifyPasswordVC.h"

@interface SuperManagerVC ()<UITableViewDelegate,UITableViewDataSource>
{
    IBOutlet UITableView *_tableView;
    IBOutlet UIActivityIndicatorView *_activity;
}
/** 静态数据 */
@property (nonatomic, strong) NSArray <NSDictionary *>*DataArr;
/** 分支机构的信息放在这里 */
/** 分支机构是否显示 */
@property (nonatomic, assign) BOOL isShow;
@property (nonatomic, strong) NSMutableArray *subDataArr;
@property (nonatomic, strong) HNSNHeaderView *headerView;

@property (nonatomic, strong) UILabel *titleView;


@property (nonatomic, copy) NSString *tempOid;
@end

@implementation SuperManagerVC

#pragma mark - life cycle

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];

    if (self.tempOid.length > 0) {

        [AccountTool shareAccountTool].currentAccount.oid = self.tempOid;
    }
}

- (void)viewDidLoad {

    [super viewDidLoad];

    self.tempOid = [NSString stringWithFormat:@"%@",[AccountTool shareAccountTool].currentAccount.oid];

    // 当网络请求成功之后调用这两条代码
    [_activity stopAnimating];
    _tableView.hidden = NO;

    self.navigationItem.titleView = self.titleView;
    self.navigationItem.titleView.alpha = 0;

    //取消滚动条
    _tableView.showsVerticalScrollIndicator = NO;
    _tableView.showsHorizontalScrollIndicator = NO;
    //添加占位View
    _tableView.tableFooterView = [UIView new];
    //设置预估高度
    _tableView.rowHeight = 44;
    //预注册
    [_tableView registerNib:[UINib nibWithNibName:[HNSNHomePageCell description] bundle:nil] forCellReuseIdentifier:@"Cell"];
    [_tableView registerNib:[UINib nibWithNibName:[SuperManagerSubCell description] bundle:nil] forCellReuseIdentifier:@"subCell"];

    UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [button setImage:[UIImage imageNamed:@"setting2"] forState:(UIControlStateNormal)];
    [button setImage:[UIImage imageNamed:@"setting2"] forState:(UIControlStateHighlighted)];
    button.frame = CGRectMake(0, 0, button.imageView.image.size.width, button.imageView.image.size.height);
    [button addTarget:self action:@selector(MgrGoModifyPassword) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];

}

/** 修改密码 */
- (void)MgrGoModifyPassword {

    ModifyPasswordVC *VC = [[ModifyPasswordVC alloc] init];
    [self.navigationController pushViewController:VC animated:YES];
}


#pragma mark - network
/** 分支机构信息请求 */
- (void)RefreshData {



}







#pragma mark - tableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return self.DataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return section < 2 ? 1:self.isShow ?  self.subDataArr.count+1:1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {

        // 第一行
        HNSNHomePageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
        cell.Dict = self.DataArr[indexPath.section];
        return cell;

    } else {

        // 分支机构的信息在这里显示
        SuperManagerSubCell *cell = [tableView dequeueReusableCellWithIdentifier:@"subCell" forIndexPath:indexPath];
        cell.model = self.subDataArr[indexPath.row-1];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {


        if (indexPath.section >= 2) {

            self.isShow = !self.isShow;
            [tableView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section] withRowAnimation:(UITableViewRowAnimationAutomatic)];

        }else if (1 == indexPath.section){
           
            
            [self.navigationController pushViewController:[ZYFundManagenmemtController  new] animated:YES];
            
            
        }else {

            [self.navigationController pushViewController:[ZYAccountManagementController new] animated:YES];
        }

    } else {

        // 分支机构
        SuperManagerModel *model = self.subDataArr[indexPath.row - 1];
        OrdinaryHomeVC *vc = [OrdinaryHomeVC new];
        vc.isSupermanager = YES;
        vc.name = model.websiteName;
        vc.picture = model.picture;
        [AccountTool shareAccountTool].currentAccount.oid = model.oid;
        [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"isCommon"];
        [self.navigationController pushViewController:vc animated:YES];





    }
}


//区头
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return section == 0 ? self.headerView:nil;
}

//区高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return section == 0 ? 150 * KScale:0;
}

#pragma mark - getter
//静态tableView的数据
- (NSArray<NSDictionary *> *)DataArr
{
    if (!_DataArr)
    {
        _DataArr = @[
                     @{@"Title":@"帐号管理",@"ImgName":@"icon_account_management_default",@"jianTou":@0},
                     @{@"Title":@"资金管理",@"ImgName":@"icon_money_default",@"jianTou":@0},
                     @{@"Title":@"分支机构",@"ImgName":@"icon_organization_default",@"jianTou":@1},
                     ];
    }
    return _DataArr;
}

- (NSMutableArray *)subDataArr {

    if (!_subDataArr) {

        _subDataArr = [NSMutableArray array];
    }
    return _subDataArr;
}

- (HNSNHeaderView *)headerView {

    if (!_headerView) {

        _headerView = [[[NSBundle mainBundle] loadNibNamed:[HNSNHeaderView description] owner:nil options:nil] lastObject];
        [_headerView RefreshData:[AccountTool shareAccountTool].currentAccount.picture];
        _headerView.HeaderName.text = [NSString stringWithFormat:@"%@(%@)",[AccountTool shareAccountTool].currentAccount.websiteName,[AccountTool shareAccountTool].currentAccount.username];
    }
    return _headerView;
}


//  在滑动过程中都会调用的方法，只要滑动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {

    CGPoint Point = scrollView.contentOffset;

    self.navigationItem.titleView.alpha = (Point.y - self.headerView.frame.size.height)*0.01;

}


- (UILabel *)titleView {

    if (!_titleView) {

        UILabel *label = [[UILabel alloc] init];
        label.text = self.headerView.HeaderName.text;
        label.font = [UIFont boldSystemFontOfSize:15.f];
        label.textColor = [UIColor whiteColor];
        label.frame = CGRectMake(0, 0, 30, 30);
        _titleView = label;
    }
    return _titleView;
}


/** 转对象 */
- (void)setDict:(NSDictionary *)dict {

    _dict = dict;
    NSArray *Arr = _dict[@"t"][@"orgInfos"];
    
    [Arr isKindOfClass:[NSNull class]] ? nil : [Arr enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        
        [self.subDataArr addObject:[SuperManagerModel HNObjectValuesForKey:obj]];
    }];
}
@end
